package march.test_3_5;


import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
        ListNode tmp = head;
        ListNode cur = head;
        Stack<Integer> stack = new Stack<>();
        while (tmp != null) {
            stack.push(tmp.val);
            tmp = tmp.next;
        }
        while (cur != null) {
            if(stack.pop() != cur.val) {
                return false;
            }
            cur = cur.next;
        }
        return true;
    }
    public int rotApple (ArrayList<ArrayList<Integer>> grid) {
        // write code here
        int n = 0;
        int[][] arr = new int[grid.size() + 2][grid.get(0).size() + 2];
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid.get(0).size(); j++) {
                arr[i + 1][j +1] = grid.get(i).get(j);
            }
        }
        while (true) {
            Boolean tmp = true;
            for(int i = 1; i <= grid.size(); i++) {
                for (int j = 1; j <= grid.get(0).size(); j++) {
                    if(arr[i][j] == 1) {
                        if (arr[i - 1][j] == 0 && arr[i + 1][j] == 0 && arr[i][j -1] == 0 && arr[i][j + 1] ==0) {
                            return -1;
                        }
                        tmp = false;
                        System.out.println("zjixing");
                    }
                    if(arr[i][j] == 2) {
                        if(arr[i - 1][j] == 1) {
                            arr[i -1][j] = 2;
                        }
                        if(arr[i + 1][j] == 1) {
                            arr[i +1][j] = 2;
                        }
                        if(arr[i][j - 1] == 1) {
                            arr[i][j - 1] = 2;
                        }
                        if(arr[i ][j + 1] == 1) {
                            arr[i ][j + 1] = 2;
                        }
                    }

                }
            }
            System.out.println(tmp);
            System.out.println(n);
            if (tmp) {
                return n;
            }
            n++;
        }
    }
}
